package com.bbs.controller;

import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import javax.websocket.server.PathParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bbs.pojo.Admin;
import com.bbs.pojo.AdminInfo;
import com.bbs.pojo.PostType;
import com.bbs.pojo.User;
import com.bbs.service.AdminService;

@Controller

public class AdminController {
	@Autowired
	private AdminService adminService;
	
	
	/*
	 * 返回等录页面
	 */
	@RequestMapping("/returnlogin")
	public String returnlogin() {
		return "login";
	}
	
	/**
	 * 跳转到管理员登录页面
	 */
	@RequestMapping("/")
	public String view() {
		return "login";
	}

//	/**
//	 * 跳转到管理员登录页面
//	 */
//	@RequestMapping("/toAdminLogin")
//
//	public String toLogin() {
//
//		return "forward:/login";
//	}

	/**
	 * 管理员登录
	 * 
	 * @param username
	 *            用户名
	 * @param password
	 *            密码
	 * @return 用户对象组成的集合
	 */
	@RequestMapping("/adminLogin")
	public String login(String username, String password, HttpServletRequest request ,HttpServletResponse response) {
	
		if ("".equals(username) || "".equals(password)) {
			
			return "/loginfail";
		}
		if (username == null || password == null) {
			return "/loginfail";
		}
		Admin admin = adminService.login(username, password);
		
		if (admin == null) {
			return "/loginfail";
		}
		if("true".equals(request.getParameter("checkbox"))){
			Cookie cookiename=new Cookie("username", username);
			cookiename.setMaxAge(60*60*24*30);//30天
			cookiename.setPath(request.getContextPath()+"/");
			Cookie cookiepass=new Cookie("password", password);
			cookiepass.setMaxAge(60*60*24*30);//30天
			cookiepass.setPath(request.getContextPath()+"/");
			response.addCookie(cookiepass);
			response.addCookie(cookiename);

		}
		HttpSession session=request.getSession();
		session.setAttribute("username", username);
		session.setAttribute("userId", admin.getId());
		return "index";
	}
	
	@RequestMapping("/returnIndex")
	public String returnIndex() {
		return "index";
	}
	
	/**
	 * 显示论坛简介（right）
	 */
	@RequestMapping("/toRight")
	public String toRight() {
		return "right";
	}
	
	
	
	
	/**
	 * 用户信息（/toUsers）
	 */
	@RequestMapping("/toUsers")
	public String toUsers() {
		return "views/design";
	}
	
	
	/**
	 * 新增（toFrom）
	 */
	@RequestMapping("/toForm")
	public String toFrom(Model model) {
		List<PostType>postList=adminService.selectTypeAll();
		System.out.println(postList);
		model.addAttribute("postList", postList);
		return "views/form";
	}
	
	/**
	 * 查询所有用户页面（toUserAll）
	 */
	@RequestMapping("/toUserAll")
	public String toUserAll() {
		return "forward:findUserAll";
	}
	

	/*
	 * 查询所有用户
	 */
	@RequestMapping("/findUserAll")
	public String findUserAll(Model model) {

		List<User> userList = adminService.findUserAll();
		model.addAttribute("userList", userList);
		return "views/users";
	}
	/*
	 *删除用户（通过用户ID） 
	 */
	@RequestMapping("/deleteUserById")
	public String deleteUserById(@PathParam("userId")String userId){
		System.out.println(userId);
		adminService.deleteUserById(userId);
		return "forward:findUserAll";
	}

	/*
	 * 根据用户名查询用户
	 */
	@RequestMapping("/selectUserByUserName")
	public String selectUserByUserName(String username,Model model) {
		System.out.println(username);
		List<User> userList = adminService.selectUserByUserName(username);
		model.addAttribute("userList", userList);
		return "views/users";
	}
	
	
	/**
	 * 查询所有管理员页面（toAdminAll）
	 */
	@RequestMapping("/toAdminAll")
	public String toAdminAll() {
		return "forward:findAdminAll";
	}
	/*
	 * 查询所有管理员详情
	 */
	@RequestMapping("/findAdminInfoAll")
	public String findAdminInfoAll(Model model) {

		List<AdminInfo> adminInfoList = adminService.findAdminInfoAll();
		model.addAttribute("adminInfoList",adminInfoList);
		return "views/manage";
	}
	
	
	/*
	 * 通过用户名查询管理员
	 */
	@RequestMapping("/selectAdminInfoByUserName")
	public String selectAdminInfoByUserName(String username,Model model) {
		System.out.println(username);
		List<AdminInfo> adminInfoList = adminService.selectAdminInfoByUserName(username);
		
		model.addAttribute("adminInfoList",adminInfoList);
		return "views/manage";
	}
	
	/*
	 * 添加分类
	 */
	@RequestMapping("/toaddPostType")
	public String toaddPostType(String postType){
		adminService.addType(postType);
		return "forward:totypelist";
	}
	
	/*
	 * 删除分类
	 */
	@RequestMapping("/deleteType")
	public String deleteType(@PathParam("type")String type){
		adminService.deleteType(type);
		adminService.deletePostType(type);
		return "forward:totypelist" ;
	}
	
	
	/*
	 * 展示所有分类/totypelist
	 */
	@RequestMapping("/totypelist")
	public String totypelist(Model model){
	
		List<PostType>postType= adminService.selectTypeAll();
		model.addAttribute("postType", postType);
		return "views/typelist";
	}
}
